import goocanvas, base
class Gene(goocanvas.Group):

    def mouse_press(self,w,e,s):
        print self.name


    def __init__(self, gene, canvas,lop,number, y):
        self.NUMBER = number
        self.BASE_PER_LINE = 2000
        self.LINE_SPACE = 1 #30
        self.BASE_WIDTH = 1
        self.BASE_HEIGHT = 0
        self.updownstream =1000
        self.no_of_lines = 1
        self.list_of_positions = lop
        self.y = y

        goocanvas.Group.__init__(self)
        self.connect("button-press-event", self.mouse_press)

        self.name = gene.name
        self.seq = gene.seq
        self.props.parent = canvas.get_root_item()
        self.render_nuc()
        self.size = (self.BASE_PER_LINE * self.BASE_WIDTH + 20 ,(self.no_of_lines + 1) * (self.LINE_SPACE + self.BASE_HEIGHT) + 1)

    def rgb(self,r,g,b):
        return "#%02x%02x%02x" % (r,g,b)

    def render_nuc(self):
        #self.titlebox = goocanvas.Rect(parent = self, x=0, y=0, width=(self.BASE_PER_LINE + 1) * self.BASE_WIDTH, height = 20, fill_color=self.rgb(209,234,211), line_width = 0)
        #self.text = goocanvas.Text(parent=self ,x=0,y=0, text =self.name, fill_color = "black", font="consolas")
        #self.titlebox.translate(0,20)
        #self.text.translate(0,20)
        j = 0
        k = 0
        l = 0
        baseno = 0
        downstream = False

        for i in range(len(self.seq)):
            motif = False
            baseno += 1
            for s in self.list_of_positions:
                if s[0]-1 < i < s[1]:
                    motif = True
            #x = base.Scheme(self, self.seq[i].upper(),motif)
            if baseno == self.updownstream:
                downstream = True
            else:
                downstream =False
            x = base.Scheme(self, self.seq[i].upper(), motif, downstream)
            x.translate(l*self.BASE_WIDTH, 40+j)
            k += 1
            l += 1

            if k > self.BASE_PER_LINE:
                self.no_of_lines +=1
                j += self.LINE_SPACE
                k = 0
                l = 0

        self.translate(10,self.y)
